﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Orgs
{
    public partial class OrgsService : IOrgsService
    {
        public ServiceResult SaveEmployee(Employee employee)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (!EntityRepository.SaveCodeEntity(employee, context))
                {
                    return new ServiceResult(false);
                }
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public ServiceResult DeleteEmployee(Guid id)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (!EntityRepository.DeleteCodeEntity<Employee>(id, context))
                {
                    return new ServiceResult(false);
                }
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public ServiceResult DeleteEmployee(string code)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (!EntityRepository.DeleteCodeEntity<Employee>(code, context))
                {
                    return new ServiceResult(false);
                }
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public SingleResult<Employee> GetEmployee(Guid id)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                return new SingleResult<Employee>(EntityRepository.GetCodeEntity<Employee>(id, context));
            }
        }

        public SingleResult<Employee> GetEmployee(string code)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                return new SingleResult<Employee>(EntityRepository.GetCodeEntity<Employee>(code, context));
            }
        }

        public ListResult<Employee> GetEmployees(List<Guid> roleIds)
        {
            return null;
        }

        public PagedList<Employee> GetEmployees(Guid orgId, int start, int limit)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                List<Employee> list = context.Set<Employee>()
                    .Join(context.EmployeePostSet.Where(p=>p.OrgId == orgId),
                        l=>l.Id,
                        r=>r.EmployeeId,
                        (l,r)=>l)
                    .OrderBy(p=>p.Code)
                    .Skip(start)
                    .Take(limit)
                    .ToList();

                return new PagedList<Employee>(list, start, limit, list.Count);
            }
        }

        public PagedList<Employee> GetEmployees(string filter, int start, int limit)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                List<Employee> list = context.Set<Employee>().Where(p => p.NameCN.Contains(filter) || p.Account.Contains(filter) || p.NameEN.Contains(filter) || p.Code.Contains(filter))
                    .OrderBy(p => p.Code)
                    .Skip(start)
                    .Take(limit)
                    .ToList();

                return new PagedList<Employee>(list, start, limit, list.Count);
            }
        }
    }
}
